Android系统su命令源码分析

这篇文章将介绍Android 4.4.4_r1 系统源码中自带的 su命令源码。

su二进行值文件一般放在Android 设备中的/system/bin 目录或/system/xbin 目录中,我们在Android 设备中的shell命令窗口中执行su 命令,当前的shell命令窗口便可获得root权限。
编译安卓系统源代码时,编译好的su二进制文件在<系统源码根目录>/out/target/product/<vendor>/system/xbin中, 生成system.img镜像文件中没有su二进制文件。su命令的源代码在<<系统源码根目录>/system/extras/su 目录下

su源码下载:http://download.csdn.net/detail/u012417380/9900329


#define LOG_TAG "su"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>

#include <unistd.h>
#include <time.h>

#include <pwd.h>

#include <private/android_filesystem_config.h>

/* 该函数根据用户名获取uid和gid */
void pwtoid(const char *tok, uid_t *uid, gid_t *gid)
{
    struct passwd *pw;
    pw = getpwnam(tok);//根据用户名获取用户登录信息
    if (pw) {
        if (uid) *uid = pw->pw_uid;
        if (gid) *gid = pw->pw_gid;
    } else {
        uid_t tmpid = atoi(tok);
        if (uid) *uid = tmpid;
        if (gid) *gid = tmpid;
    }
}
//根据用户名提取uid、groupid
void extract_uidgids(const char *uidgids, uid_t *uid, gid_t *gid, gid_t *gids,
                     int *gids_count)
{
    char *clobberablegids;
    char *nexttok;
    char *tok;
    int gids_found;

    if (!uidgids || !*uidgids) {
        *gid = *uid = 0;
        *gids_count = 0;
        return;
    }
    clobberablegids = strdup(uidgids);
    strcpy(clobberablegids, uidgids);
    nexttok = clobberablegids;
    tok = strsep(&nexttok, ",");
    pwtoid(tok, uid, gid);
    tok = strsep(&nexttok, ",");
    if (!tok) {
        /* gid is already set above */
        *gids_count = 0;
        free(clobberablegids);
        return;
    }
    pwtoid(tok, NULL, gid);
    gids_found = 0;
    while ((gids_found < *gids_count) && (tok = strsep(&nexttok, ","))) {
        pwtoid(tok, NULL, gids);
        gids_found++;
        gids++;
    }
    if (nexttok && gids_found == *gids_count) {
        fprintf(stderr, "too many group ids\n");
    }
    *gids_count = gids_found;
    free(clobberablegids);
}

/*
 * SU can be given a specific command to exec. UID _must_ be
 * specified for this (ie argc => 3).
 *
 * Usage:
 *   su 1000
 *   su 1000 ls -l
 *  or
 *   su [uid[,gid[,group1]...] [cmd]]
 *  E.g.
 *  su 1000,shell,net_bw_acct,net_bw_stats id
 * will return
 *  uid=1000(system) gid=2000(shell) groups=3006(net_bw_stats),3007(net_bw_acct)
 */
int main(int argc, char **argv)
{
    struct passwd *pw;
    uid_t uid, myuid;
    gid_t gid, gids[10];

    /* Until we have something better, only root and the shell can use su. */
    myuid = getuid();
    if (myuid != AID_ROOT && myuid != AID_SHELL) {
        fprintf(stderr,"su: uid %d not allowed to su\n", myuid);
        return 1;
    }

    if(argc < 2) {
        uid = gid = 0;
    } else {
        int gids_count = sizeof(gids)/sizeof(gids[0]);
        extract_uidgids(argv[1], &uid, &gid, gids, &gids_count);
        if(gids_count) {
            if(setgroups(gids_count, gids)) {
                fprintf(stderr, "su: failed to set groups\n");
                return 1;
            }
        }
    }
    //设置当前用户的有效id为之前提取到的用户id
    if(setgid(gid) || setuid(uid)) {
        fprintf(stderr,"su: permission denied\n");
        return 1;
    }

    /* User specified command for exec. */
    if (argc == 3 ) {
        if (execlp(argv[2], argv[2], NULL) < 0) {
            int saved_errno = errno;
            fprintf(stderr, "su: exec failed for %s Error:%s\n", argv[2],
                    strerror(errno));
            return -saved_errno;
        }
    } else if (argc > 3) {
        /* Copy the rest of the args from main. */
        char *exec_args[argc - 1];
        memset(exec_args, 0, sizeof(exec_args));
        memcpy(exec_args, &argv[2], sizeof(exec_args));
        if (execvp(argv[2], exec_args) < 0) {
            int saved_errno = errno;
            fprintf(stderr, "su: exec failed for %s Error:%s\n", argv[2],
                    strerror(errno));
            return -saved_errno;
        }
    }

    /* Default exec shell. */
    execlp("/system/bin/sh", "sh", NULL);

    fprintf(stderr, "su: exec failed\n");
    return 1;
}
Android系统开发是指使用Android平台进行应用程序开发的过程。在Android系统开发中,开发者可以利用Android提供的开发工具和框架来创建移动应用程序,这些应用程序可以运行在Android设备上。 Android系统开发主要包括以下几个方面: 1. 硬件要求:Android系统开发需要开发者了解Android设备的硬件要求和限制,以便在应用程序开发过程中能够充分利用设备的功能和性能。 2. 开发环境搭建:开发者需要安装Android开发工具包(Android SDK),其中包括Android Studio集成开发环境(IDE),以及相关的开发工具和库。通过Android Studio,开发者可以创建、调试和测试Android应用程序。 3. 应用程序结构:Android应用程序采用一种基于组件的架构模式。开发者可以通过定义活动(Activity)、服务(Service)、广播接收器(Broadcast Receiver)和内容提供器(Content Provider)等组件来构建应用程序。 4. 用户界面设计:Android系统提供了丰富的用户界面组件,开发者可以使用这些组件来创建各种交互界面,包括布局、视图和对话框等。 5. 数据存储和管理:Android系统提供了多种数据存储和管理方式,开发者可以选择适合应用程序需求的存储方式,包括数据库(SQLite)、文件系统和SharedPreferences等。 6. 资源管理:Android系统支持多种类型的资源管理,包括图像、音频、字符串和布局等。开发者可以将这些资源文件添加到应用程序中,并在代码中引用和使用。 7. 应用程序发布:完成应用程序开发后,开发者可以利用Android开发工具包提供的工具和服务将应用程序打包、签名并发布到Google Play商店或其他应用分发平台,供用户下载和安装。 总结起来,Android系统开发是一项涉及多个方面的复杂任务,需要开发者具备一定的编程和移动应用开发经验,并熟悉Android平台的开发工具和框架。通过合理利用Android系统提供的功能和资源,开发者可以创建出功能强大、用户友好的Android应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值